package com.itg.vms.servlet;

import com.itg.vms.entity.Movies;
import com.itg.vms.entity.User;
import com.itg.vms.service.MoviesService;
import com.itg.vms.service.impl.MoviesServiceImpl;
import com.itg.vms.util.MD5Utils;
import com.itg.vms.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/api/movies")
public class MoviesServlet extends BaseServlet{

    MoviesService moviesService=new MoviesServiceImpl();
    /**
     * 后台添加视频信息
     */
    public boolean addMovies(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //接收提交的值
        String movie_Picture = req.getParameter("movie_Picture");
        String movie_ChineseName = req.getParameter("movie_ChineseName");
        String movie_Name = req.getParameter("movie_Name");
        String movie_Age = req.getParameter("movie_Age");
        String movie_Place = req.getParameter("movie_Place");
        String movie_Type = req.getParameter("movie_Type");
        String movie_Language = req.getParameter("movie_Language");
        String movie_Subtitle = req.getParameter("movie_Subtitle");
        String movie_ReleaseDate = req.getParameter("movie_ReleaseDate");
        String movie_IMDbScore = req.getParameter("movie_IMDbScore");
        String movie_DBScore = req.getParameter("movie_DBScore");
        String movie_Times = req.getParameter("movie_Times");
        String movie_Director = req.getParameter("movie_Director");
        String movie_Scriptwriter = req.getParameter("movie_Scriptwriter");
        String movie_Actor = req.getParameter("movie_Actor");
        String movie_Introduction = req.getParameter("movie_Introduction");
        String movie_Link = req.getParameter("movie_Link");
        Movies movies=new Movies(movie_Picture,movie_ChineseName,movie_Name,movie_Age,movie_Place,movie_Type,movie_Language,
                movie_Subtitle,movie_ReleaseDate,movie_IMDbScore,movie_DBScore,movie_Times,movie_Director,movie_Scriptwriter,
                movie_Actor,movie_Introduction,movie_Link);

        movies.setCreateBy(getUsername(req, resp));

        //调用userService方法
        boolean flag = moviesService.addMovies(movies);
        if (flag) {
            resp.getWriter().write(success("添加成功"));
        } else {
            resp.getWriter().write(error());
        }
        return flag;

    }
    /**
     * 删除视频信息
     */
    public void deleteMoviesById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String movie_Id = req.getParameter("movie_Id");
        int i = moviesService.deleteMoviesById(movie_Id);
        resp.getWriter().write(toAjax(i));
    }
    //修改视频信息
    public boolean UpdateMoviesById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String movie_Picture = req.getParameter("movie_Picture");
        String movie_ChineseName = req.getParameter("movie_ChineseName");
        String movie_Name = req.getParameter("movie_Name");
        String movie_Age = req.getParameter("movie_Age");
        String movie_Place = req.getParameter("movie_Place");
        String movie_Type = req.getParameter("movie_Type");
        String movie_Language = req.getParameter("movie_Language");
        String movie_Subtitle = req.getParameter("movie_Subtitle");
        String movie_ReleaseDate = req.getParameter("movie_ReleaseDate");
        String IMDbScore = req.getParameter("IMDbScore");
        String DBScore = req.getParameter("DBScore");
        String movie_Times = req.getParameter("movie_Times");
        String movie_Director = req.getParameter("movie_Director");
        String movie_Scriptwriter = req.getParameter("movie_Scriptwriter");
        String movie_Actor = req.getParameter("movie_Actor");
        String movie_Introduction = req.getParameter("movie_Introduction");
        String movie_Link = req.getParameter("movie_Link");
        String movie_Id = req.getParameter("id");
        Integer integermovie_Id = Integer.valueOf(movie_Id);
        boolean i = moviesService.UpdateMoviesById(movie_Picture,movie_ChineseName,movie_Name,movie_Age,movie_Place,
                movie_Type,movie_Language, movie_Subtitle,movie_ReleaseDate,IMDbScore,DBScore,
                movie_Times,movie_Director,movie_Scriptwriter, movie_Actor,movie_Introduction,movie_Link
                ,integermovie_Id);

        if (i) {
            resp.getWriter().write(success("修改成功"));
        } else {
            resp.getWriter().write(error());
        }
        return i;
    }
    //根据视频名称查询视频信息
    public void selectMoviesByName(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String movie_Name = req.getParameter("movie_Name");

        List<Movies> list = moviesService.selectMoviesByName( movie_Name);
        if (list != null) {
            resp.getWriter().write(success(list, Long.valueOf(list.size())));
        } else {
            resp.getWriter().write(error());
        }
    }
    //显示视频具体信息
    public void DisplayMoviesById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String movie_id = req.getParameter("id");
        Movies movies= moviesService.DisplayMoviesById(movie_id);
        if(movies!=null){
            resp.getWriter().write(success(movies));
        }else{
            resp.getWriter().write(error());
        }
    }
    //展示所有视频信息
    public void selectAllMovies(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //得到请求参数
        String page = req.getParameter("page");
        Integer currentPage = Integer.valueOf(page);
        String limit = req.getParameter("limit");
        Integer pageSize = Integer.valueOf(limit);
        //当前页面数减1，mysql分页从第0也开始
        currentPage = currentPage - 1;
        //调用业务层方法查询数据
        List<Movies> movies = moviesService.selectAllMovies(currentPage, pageSize);
        //得到movies表的总记录数
        Long allMoviesSize = moviesService.getAllMoviesSize();
        //判断这个列表是否为null，为null查询失败
        if (StringUtils.isNotNull(movies)) {
            //这个success方法和下面的error方法均写在BaseServlet中，用于返回json格式的数据，使用时只需要传递对象
            //Collections.singletonList 返回仅包含指定对象的不可变列表。返回的列表是可序列化的。

            System.out.println(success(movies, allMoviesSize));
            resp.getWriter().write(success(movies, allMoviesSize));
        } else {
            resp.getWriter().write(error("查询失败"));
        }
    }
}
